﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;
using System.Linq;

namespace AllTalk.Data
{
    public enum PictureType
    {
        None,
        Jpg,
        Png
    }

    public class Picture
    {
        internal Friend Friend { get; private set; }

        internal PictureType Type { get; private set; }

        internal ImageSource Source { get; private set; }

        private static string BasePath = "Images";

        internal string Filename 
        { 
            get
            {
                return String.Concat(BasePath, @"\", this.Friend.Email, this.Friend.PictureHash);
            }
        }

        internal Picture(Friend friend)
        {
            this.Friend = friend;            
        }

        internal bool LoadPicture()
        {
            if (App.CachePhotos)
            { 
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(this.Filename))
                    {
                        using (var stream = store.OpenFile(this.Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            BitmapImage image = new BitmapImage();
                            image.SetSource(stream);
                            this.Source = image;
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        internal void Save(XElement vcardPicture)
        {
            this.Type = GetType(vcardPicture.Element(NamespaceConstants.VCardNamespace + "TYPE").Value);
            byte[] imageBuffer = Convert.FromBase64String(vcardPicture.Element(
                NamespaceConstants.VCardNamespace + "BINVAL").Value);

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists(BasePath))
                {
                    store.CreateDirectory(BasePath);
                }
                using (var stream = store.OpenFile(this.Filename, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    stream.Write(imageBuffer, 0, imageBuffer.Length);
                    BitmapImage image = new BitmapImage();
                    using(MemoryStream memoryStream = new MemoryStream(imageBuffer))
                    {
                        image.SetSource(memoryStream);
                        this.Source = image;
                    }
                }
            }
        }

        private static PictureType GetType(string pictureType)
        {
            switch(pictureType)
            {
                case "image/jpg":
                case "image/jpeg":
                    return PictureType.Jpg;
                case "image/png":
                    return PictureType.Png;
                default:
                    Debug.Assert(false, "Unsupported picture type");
                    return PictureType.None;
            }
        }
    }

}
